﻿Public Class frmLEDGauge
    Private Enum GaugeAnimation As Integer
        Increase = 0
        Decrease = 1
    End Enum
    Private _AnimateGaugeDirection As GaugeAnimation = GaugeAnimation.Increase

    Private Sub frmLEDGauge_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LedGauge1.Value = 0
        _AnimateGaugeDirection = GaugeAnimation.Increase
        Timer1.Enabled = False
        Timer1.Interval = 10
        btnStartGauge.Text = "Start Gauge"

        'Set up data sources from FillStyle and FillGradientStyle combo boxes
        cboFillGradientStyle.DataSource = System.Enum.GetValues(GetType(System.Drawing.Drawing2D.LinearGradientMode))
        cboFillStyle.DataSource = System.Enum.GetValues(GetType(PolyMonControls.LEDGauge.BackgroundType.FillStyles))
        cboTextLabelPosition.DataSource = System.Enum.GetValues(GetType(PolyMonControls.LEDGauge.GaugeLabelPositions))
        cboValueLabelPosition.DataSource = System.Enum.GetValues(GetType(PolyMonControls.LEDGauge.GaugeValueLabel.GaugeValuePositions))
        cboStyle.DataSource = System.Enum.GetValues(GetType(PolyMonControls.LEDGauge.LEDType.LEDStyles))

        'Bind Form objects to Gradient Panel Properties
        cboFillGradientStyle.DataBindings.Add("SelectedItem", LedGauge1.Background, "FillGradientStyle")
        cboFillStyle.DataBindings.Add("SelectedItem", LedGauge1.Background, "FillStyle")
        pbBackColor1.DataBindings.Add("BackColor", LedGauge1.Background, "BackColor1")
        pbBackColor2.DataBindings.Add("BackColor", LedGauge1.Background, "BackColor2")


        nudPaddingTop.DataBindings.Add("Value", LedGauge1.Padding, "Top")
        nudPaddingBottom.DataBindings.Add("Value", LedGauge1.Padding, "Bottom")
        nudPaddingLeft.DataBindings.Add("Value", LedGauge1.Padding, "Left")
        nudPaddingRight.DataBindings.Add("Value", LedGauge1.Padding, "Right")

        cboTextLabelPosition.DataBindings.Add("SelectedItem", LedGauge1, "LabelPosition")
        pbForeColor.DataBindings.Add("BackColor", LedGauge1, "ForeColor")
        pgFont.DataBindings.Add("SelectedObject", LedGauge1, "Font")


        cboValueLabelPosition.DataBindings.Add("SelectedItem", LedGauge1.ValueLabel, "LabelPosition")
        pbValueLabelColor.DataBindings.Add("BackColor", LedGauge1.ValueLabel, "ForeColor")
        pgValueLabelFont.DataBindings.Add("SelectedObject", LedGauge1.ValueLabel, "Font")

        cboStyle.DataBindings.Add("SelectedItem", LedGauge1.LEDStyle, "Style")
        pbOffColor.DataBindings.Add("BackColor", LedGauge1.LEDStyle, "OffColor")
        pbOnColor.DataBindings.Add("BackColor", LedGauge1.LEDStyle, "OnColor")
        nudCount.DataBindings.Add("Value", LedGauge1.LEDStyle, "Count")
        nudGap.DataBindings.Add("Value", LedGauge1.LEDStyle, "Gap")
        pbT1Color.DataBindings.Add("BackColor", LedGauge1.Threshold1, "Color")
        nudT1Value.DataBindings.Add("Value", LedGauge1.Threshold1, "Value")
        pbT2Color.DataBindings.Add("BackColor", LedGauge1.Threshold2, "Color")
        nudT2Value.DataBindings.Add("Value", LedGauge1.Threshold2, "Value")

        nudRangeMin.DataBindings.Add("Value", LedGauge1.Range, "Min")
        nudRangeMax.DataBindings.Add("Value", LedGauge1.Range, "Max")


        'Add handlers for color selections
        AddHandler pbBackColor1.Click, AddressOf ChooseColor
        AddHandler pbBackColor2.Click, AddressOf ChooseColor
        AddHandler pbForeColor.Click, AddressOf ChooseColor
        AddHandler pbValueLabelColor.Click, AddressOf ChooseColor
        AddHandler pbOffColor.Click, AddressOf ChooseColor
        AddHandler pbOnColor.Click, AddressOf ChooseColor
        AddHandler pbT1Color.Click, AddressOf ChooseColor
        AddHandler pbT2Color.Click, AddressOf ChooseColor


        'Add Handler for Form object property changes
        AddHandler cboFillStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler cboFillGradientStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler pbBackColor1.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbBackColor2.BackColorChanged, AddressOf ControlValueChanged


        AddHandler nudPaddingTop.ValueChanged, AddressOf UpdatePadding
        AddHandler nudPaddingBottom.ValueChanged, AddressOf UpdatePadding
        AddHandler nudPaddingLeft.ValueChanged, AddressOf UpdatePadding
        AddHandler nudPaddingRight.ValueChanged, AddressOf UpdatePadding

        AddHandler cboTextLabelPosition.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler pbForeColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pgFont.PropertyValueChanged, AddressOf ControlValueChanged

        AddHandler cboValueLabelPosition.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler pbValueLabelColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pgValueLabelFont.PropertyValueChanged, AddressOf ControlValueChanged

        AddHandler cboStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler pbOffColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbOnColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler nudCount.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudGap.ValueChanged, AddressOf ControlValueChanged
        AddHandler pbT1Color.BackColorChanged, AddressOf ControlValueChanged
        AddHandler nudT1Value.ValueChanged, AddressOf ControlValueChanged
        AddHandler pbT2Color.BackColorChanged, AddressOf ControlValueChanged
        AddHandler nudT2Value.ValueChanged, AddressOf ControlValueChanged

        AddHandler nudRangeMin.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudRangeMax.ValueChanged, AddressOf ControlValueChanged

    End Sub

    Private Sub btnStartGauge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartGauge.Click
        If Timer1.Enabled Then
            Timer1.Enabled = False
            btnStartGauge.Text = "Start Gauge"
        Else
            Timer1.Enabled = True
            btnStartGauge.Text = "Stop Gauge"
        End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If LedGauge1.Value >= LedGauge1.Range.Max AndAlso _AnimateGaugeDirection = GaugeAnimation.Increase Then
            _AnimateGaugeDirection = GaugeAnimation.Decrease
            LedGauge1.Value = LedGauge1.Range.Max
        ElseIf LedGauge1.Value <= LedGauge1.Range.Min AndAlso _AnimateGaugeDirection = GaugeAnimation.Decrease Then
            _AnimateGaugeDirection = GaugeAnimation.Increase
            LedGauge1.Value = LedGauge1.Range.Min
        End If

        Select Case _AnimateGaugeDirection
            Case GaugeAnimation.Increase
                LedGauge1.Value += 1
            Case GaugeAnimation.Decrease
                LedGauge1.Value -= 1
        End Select
    End Sub

    Private Sub ChooseColor(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim pb As PictureBox = CType(sender, PictureBox)
        With Me.ColorDialog1
            .Color = pb.BackColor
            Dim res As DialogResult = .ShowDialog()

            If res = Windows.Forms.DialogResult.OK Then
                pb.BackColor = .Color
            End If
        End With
    End Sub

    Private Sub ControlValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Ctl As Control = CType(sender, Control)
        If Ctl.DataBindings.Count > 0 Then
            Ctl.DataBindings(0).WriteValue()
        End If
    End Sub

    Private Sub UpdatePadding()
        LedGauge1.Padding = New Padding(nudPaddingLeft.Value, nudPaddingTop.Value, nudPaddingRight.Value, nudPaddingBottom.Value)
    End Sub
End Class